﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyBet.Web.DAL;
using System.Web.Security;
using System.Web.Profile;
using System.ComponentModel.DataAnnotations;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
using System.Xml.Serialization;
using System.Runtime.Serialization;

namespace MyBet.Web
{
    [DataContractAttribute(IsReference = true)]
    [Serializable()]
    public class TeamObject
    {
        private int _teamId;
        private string _name;
        private byte[] _image;

        [Key()]
        [DataMemberAttribute()]
        public int TeamId
        {
            get { return _teamId; }
            set { _teamId = value; }
        }

        [DataMemberAttribute()]
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        [DataMemberAttribute()]
        public byte[] Image
        {
            get { return _image; }
            set { _image = value; }
        }

        /// <summary>
        /// Create Team from database object
        /// </summary>
        /// <param name="t">database object</param>
        /// <returns></returns>
        public static TeamObject CopyFrom(Teams t)
        {
            TeamObject team = new TeamObject();
            team.TeamId = t.TeamId;
            team.Name = t.Name.Trim();
            team.Image = t.Icon;
            return team;
        }

        /// <summary>
        ///  Get team identifier from name
        /// </summary>
        /// <param name="teamName"></param>
        /// <returns></returns>
        public static int GetTeamId(MyBetDBEntities dbContext, string teamName)
        {
            var query = (from t in dbContext.Teams
                         where t.Name.ToUpper() == teamName.ToUpper()
                         select t.TeamId);
            IList<int> teamsId = query.ToList();
            if (teamsId.Count != 1)
            {
                // unknown team
                return -1;
            }
            return teamsId[0];
        }

        public static List<TeamObject> LoadTeams()
        {
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                List<TeamObject> retList = new List<TeamObject>();
                var query = (from team in dbContext.Teams
                             select team);
                IEnumerable<Teams> teams = query.ToList();
                foreach (Teams t in teams)
                {
                    retList.Add(TeamObject.CopyFrom(t));
                }
                return retList;
            }
        }

        public static List<Teams> Load()
        {
            using (MyBetDBEntities dbContext = new MyBetDBEntities())
            {
                List<TeamObject> retList = new List<TeamObject>();
                var query = (from team in dbContext.Teams
                             select team);
                List<Teams> teams = query.ToList();
                return teams;
            }
        }

    }
}